<?php


namespace app\services;


use app\model\User;
use sms\Verify;
use think\exception\HttpException;
use think\facade\Cache;
use utils\AliVerify;

class SendCode
{

    protected $url = 'http://smscc.icloudata.net:18899/HttpMtInfo/save.do';

    public function __construct()
    {
        $this->redis = Cache::store('redis');
    }


    /**
     * 发送短信
     * @param $phone
     * @param $type
     * @param $ali_verify
     * @return bool
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function SendCode($phone, $type, $ali_verify = '')
    {
        switch ($type) {
            case  1:
                if (empty($ali_verify)) {
                    return app('json')->json_error('发送失败');
                }
                $ali_verify = json_decode(html_entity_decode($ali_verify), true);
                $verify = app()->make(AliVerify::class);
                if (!$verify->ali_verify($ali_verify['appkey'], $ali_verify['scene'], $ali_verify['csessionid'], $ali_verify['nc_token'], $ali_verify['sig'])) {
                    throw new HttpException(500, '发送失败');
                }
                $redis_type = '_RegisterCode';
                break;
            case  2:

                $redis_type = '_UpdateCode';
                $u = User::where(['phone' => $phone])->find();
                if (!$u) {
                    throw new HttpException(500, '用户不存在');
                }
                break;
            case 3:
                $redis_type = '_Login';
                $u = User::where(['phone' => $phone])->find();
                if (!$u) {
                    throw new HttpException(500, '用户不存在');
                }
                break;
        }
        $code = str_pad(mt_rand(0, 999999), 4, "0", STR_PAD_BOTH);
        $this->redis->set($phone . $redis_type, $code, 120);
        $time = date('YmdHis', time()) . mb_substr(time(), -4);
        $arr = [
            "username" => "cjxyzm",
            "sign" => strtoupper(md5('cjxyzmcjxyzm@11' . $time)),
            "timestamp" => date('YmdHis', time()) . mb_substr(time(), -4),
            "messagelist" => [
                [
                    "content" => urlencode('【源聚变】您的验证码是' . $code),
                    "mobile" => $phone,
                ],
            ]
        ];
        $result = CurlJson($this->url, $arr);
        $result = json_decode($result, true);
        if ($result['resultdesc'] != 'SUCCESS') {
            throw new HttpException(500, '发送失败');
        }
        return true;
    }


    /**
     * 验证短信
     * @param $code
     * @param $phone
     * @return bool
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function CodeVerify($code, $phone, $type)
    {
        switch ($type) {
            case  1:
                $redis_type = '_RegisterCode';
                break;
            case  2:
                $redis_type = '_UpdateCode';
                break;
            case 3:
                $redis_type = '_Login';
                break;
        }
        $redis_code = $this->redis->get($phone . $redis_type);
        if ($code != '111111') {
            if ($redis_code != $code) {
                throw new HttpException(500, '验证码错误');
            }
        }
        return true;
    }
}